package api

import (
	"exam12_23/internal/api/request"
	"exam12_23/internal/models/service"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
)

func CreateFriend(c *gin.Context) {
	var json *request.Friend
	if err := c.ShouldBindJSON(&json); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	value, _ := c.Get("userId")
	userId, _ := strconv.Atoi(value.(string))

	friend := service.CreateFriend(json, userId)

	if friend != "" {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    400,
			"message": friend,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "好友申请发送成功",
	})

}

func YesFriend(c *gin.Context) {
	value, _ := c.Get("userId")
	userId, _ := strconv.Atoi(value.(string))

	yes := service.Yes(userId)

	if yes != "" {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    400,
			"message": yes,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "好友添加成功",
	})

}

func NoFriend(c *gin.Context) {
	value, _ := c.Get("userId")
	userId, _ := strconv.Atoi(value.(string))

	no := service.No(userId)

	c.JSON(http.StatusOK, gin.H{
		"message": no,
		"code":    200,
	})

}
